package Snake_Game;

import Snake_Game.obj.BodyObj;
import Snake_Game.obj.FoodObj;
import Snake_Game.obj.HeadObj;
import Snake_Game.utils.GameUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

public class GameWin extends JFrame {


    //游戏状态 0.未开始 1.游戏中 2.暂停 3.失败 4.通关 5.失败后重启 6.下一关
    public static int state = 0;
    //分数
    public int score = 0;
    //双缓存图片
    Image offScreenImage = null;
    //窗口长宽
    int winWidth = 800;
    int winHeight = 600;

    //蛇头对象
    HeadObj headObj = new HeadObj(GameUtils.rightImg,60,570,this);

    //蛇身集合
    public List<BodyObj> bodyObjList = new ArrayList<>();

    //食物对象
    public FoodObj foodObj = new FoodObj().getFood();

    public void launch(){
        //窗口可视
        this.setVisible(true);
        this.setSize(winWidth,winHeight);
        //居中
        this.setLocationRelativeTo(null);
        this.setTitle("贪吃蛇小游戏");

        //蛇身初始化
        bodyObjList.add(new BodyObj(GameUtils.bodyImg,30,570,this));
        bodyObjList.add(new BodyObj(GameUtils.bodyImg,0,570,this));

        //键盘事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //获取空格输入
                if(e.getKeyCode() == KeyEvent.VK_SPACE){
                    switch (state){
                        //未开始或已暂停
                        case 0, 2:
                            state = 1;
                            break;
                        //游戏中
                        case 1:
                            state = 2;
                            repaint();
                            break;
                        case 3:
                            state = 5;
                            break;
                        case 4:
                            state = 6;
                            break;
                        default:
                            break;
                    }
                }
            }
        });

        while (true){
            if(state == 1){
                repaint();
            } else if (state == 5) {
                state = 0;
                reSetGame();
            }else if (state == 6 && GameUtils.level != 3) {
                GameUtils.level++;
                state = 1;
                reSetGame();
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        //舒适化双缓存图片
        if(offScreenImage == null){
            offScreenImage = this.createImage(winWidth,winHeight);
        }
        //获取图片对应的graphics对象
        Graphics gImage = offScreenImage.getGraphics();
        //灰色背景
        gImage.setColor(Color.gray);
        //填充矩形
        gImage.fillRect(0,0,winWidth,winHeight);
        gImage.setColor(Color.black);
        for (int i = 1; i <= 20; i++) {
            gImage.drawLine(0,i * 30,600,i * 30);
            gImage.drawLine(i * 30,0,i * 30,600);
        }
        
        //绘制蛇身
        for (int i = bodyObjList.size() - 1; i >= 0; i--) {
            bodyObjList.get(i).paintSelf(gImage);
        }
        
        //绘制蛇头
        headObj.paintSelf(gImage);

        //绘制食物
        foodObj.paintSelf(gImage);

        //
        GameUtils.drawWord(gImage,"第" + GameUtils.level + "关",Color.orange,40,650,260);

        //绘制分数
        GameUtils.drawWord(gImage,score + " 分",Color.BLUE,50,650,330);

        //绘制提示语
        g.setColor(Color.gray);
        prompt(gImage);
        //将双缓存的图片绘制到主窗口中
        g.drawImage(offScreenImage,0,0,null);
    }

    //绘制提示语
    void prompt(Graphics g){
        //未开始
        if(state == 0){
            g.fillRect(120,240,400,70);
            GameUtils.drawWord(g,"按下空格开始游戏",Color.YELLOW,35,150,290);
        } else if(state == 2){
            g.fillRect(120,240,400,70);
            GameUtils.drawWord(g,"按下空格继续游戏",Color.YELLOW,35,150,290);
        } else if (state == 3) {
            g.fillRect(120,240,400,70);
            GameUtils.drawWord(g,"撞到障碍游戏失败",Color.red,35,150,290);
        }else if (state == 4) {
            g.fillRect(120,240,400,70);
            if(GameUtils.level == 3){
                GameUtils.drawWord(g,"条件达成，游戏通关",Color.green,35,150,290);
            }else {
                GameUtils.drawWord(g,"条件达成，空格前往下一关",Color.green,35,150,290);
            }
        }
    }

    void reSetGame(){
        //关闭当前窗口
        this.dispose();
        //开启新窗口
        String[] args = {};
        main(args);
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
